package org.go.matchers;

import org.go.util.Key;

/**
 * Matches on the complete key being equal (both name and group). 
 *  
 * @author jhouse
 */
public class KeyMatcher<T extends Key> implements Matcher<T> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8013579395019617844L;

	/**
	 * Create a KeyMatcher that matches Keys that equal the given key. 
	 */
	public static <U extends Key> KeyMatcher<U> keyEquals(U compareTo) {
		return new KeyMatcher<U>(compareTo);
	}

	protected T compareTo;

	protected KeyMatcher(T compareTo) {
		this.compareTo = compareTo;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		KeyMatcher other = (KeyMatcher) obj;
		if (compareTo == null) {
			if (other.compareTo != null)
				return false;
		} else if (!compareTo.equals(other.compareTo))
			return false;
		return true;
	}

	public T getCompareToValue() {
		return compareTo;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((compareTo == null) ? 0 : compareTo.hashCode());
		return result;
	}

	@Override
	public boolean isMatch(T key) {

		return compareTo.equals(key);
	}

}
